package com.fcy.teamwork.business.projectlife

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fcy.teamwork.R
import com.fcy.teamwork.base.toast
import com.fcy.teamwork.databinding.FrgAddNewPlanNodeBinding
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class AddNewPlanNodeFrag : BottomSheetDialogFragment() {
    private lateinit var binding: FrgAddNewPlanNodeBinding
    private val vm: ProjectControllerVM by activityViewModels()
    private val adapter by lazy { OwnerListAdapter(vm.projectInfo) }
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return BottomSheetDialog(requireContext(), R.style.BottomSheetStyle)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FrgAddNewPlanNodeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.rvOwners.also {
            it.layoutManager = LinearLayoutManager(it.context, RecyclerView.HORIZONTAL, false)
            it.adapter = adapter
            adapter.manager = activity?.supportFragmentManager!!
        }
        binding.actionBar.tvTitle.text = "添加新的节点"
        binding.actionBar.ibFinish.apply {
            visibility = View.VISIBLE
            setOnClickListener {
                if (checkParams()) {
                    requireContext().toast("节点信息不完整")
                }
                // 将创建的节点信息上传到服务端 并在刷新整个project的所有节点
                vm.buildingBode.apply {
                    owners = vm.selectedMembers.foldRightIndexed("") { index, person, acc ->
                        if (index == 0)
                            person.personId
                        else acc.plus(",").plus(person.personId)
                    }
                    beginTmp = System.currentTimeMillis()
                    title = binding.etTitle.text.toString()
                }
                vm.updateNodeInfo(vm.buildingBode)

                dismiss()
            }
        }
        lifecycleScope.launch {
            vm.selectFlow.collect {
                adapter.submitData(it)
            }
        }
    }

    /**
     *
     * @return Boolean true 有问题
     */
    private fun checkParams(): Boolean {
        return binding.etTitle.text.toString().isBlank() || vm.selectedMembers.isEmpty()
    }
}